#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
class Solution {
  bool calculateOverlap(int l1, int r1, int l2, int r2, int &overlapL,
                        int &overlapR) {
    // 计算可能的重合部分：
    // 左端点是两个线段左端点的较大值
    // 右端点是两个线段右端点的较小值
    overlapL = std::max(l1, l2);
    overlapR = std::min(r1, r2);

    // 检查是否真的有重合部分
    return overlapL <= overlapR;
  }

public:
  int findMinArrowShots(vector<vector<int>> &points) {
    std::sort(points.begin(), points.end(),
              [](const vector<int> &a, const vector<int> &b) -> bool {
                if (a[0] != b[0]) {
                  return a[0] < b[0];
                } else {
                  return a[1] < b[1];
                }
              });
    for (int i = 0; i < points.size(); ++i) {
      cout << "l: " << points[i][0] << " " << "r: " << points[i][1] << endl;
    }
    int res = 1;
    int begins = points[0][0];
    int ends = points[0][1];
    for (int i = 1; i < points.size(); ++i) {
      int mbegin = 0;
      int mend = 0;
      if (calculateOverlap(begins, ends, points[i][0], points[i][1], mbegin,
                           mend)) {
        begins = mbegin;
        ends = mend;
      } else {
        ++res;
        begins = points[i][0];
        ends = points[i][1];
      }
    }
    return res;
  }
};

int main(int argc, char *argv[]) {

  vector<vector<int>> a = {{10, 16}, {2, 8}, {1, 6}, {7, 12}};
  Solution s;
  cout<<s.findMinArrowShots(a)<<endl;

  return 0;
}
